#!/bin/bash

# this script is the demo for bash conditional mechanism

## if block

# process:
# 1. the shell runs the command after 'if' keyword and collect the exit code of that command
# 2. if the exit code is 0, the shell executes the commandss that after 'then' key word,
#    stopping when it reaches an 'else' or 'fi' keyword
# 3. if the exit code is not 0 and there's an else clause, the shell runs the commands after the 'else'
# 4. the conditional ends at 'fi' 
if [ "$1" = "Hi" ]; then
    echo 'the first argument was "Hi"';
elif [ "$2" = "bye" ]; then 
    echo 'the second argument was "Bye"';
fi

## other commands for tests

if grep -q daemon /etc/passwd; then
    echo 'the daemon user is in the passwd file.'
else 
    echo 'daemon is not in the passwd file.'
fi


## logical constructs

# && AND operator, || OR operator
# &&: shell runs command1, if the exit code is 0, the 
#     shell runs command2. only two command with exit code 
#     0, shell continue runs the command after 'then' keyword
# ||: shell runs command1, if command finished with non-zero
#     exit code ,the shell runs command2

# pattern 1
if [ "$1" = "yes" ] && [ "$2" = "!" ]; then 
    echo "your input correctly."
else 
    echo "invalid input"
fi

# pattern 2: -o for OR, -a for AND
if [ "$1" = "yes" -o "$2" = "!" ]; then
    echo "your input correctly.";
else 
    echo "invalid input";
fi
